/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.serv.transfer.service;

import com.synjones.pay.bank.api.IBankDirector;
import com.synjones.pay.bank.ecard.service.EcardDirectorImpl;
import com.synjones.pay.model.common.ResponseBean;
import com.synjones.pay.model.context.IPayContext;
import com.synjones.pay.model.context.StatusInfo;
import com.synjones.pay.model.transfer.TransferReqBean;
import com.synjones.pay.model.transfer.TransferRespBean;
import com.synjones.pay.serv.dao.bean.TransferDao;
import com.synjones.pay.serv.dao.repository.TansferDaoRepository;
import com.synjones.pay.serv.transfer.helper.TransferHelper;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 *
 * @author hsd
 */
@Service("transferService")
public class TransferServiceImpl implements ITransferService {

    private final static Logger logger = LoggerFactory.getLogger(TransferServiceImpl.class);

    @Resource
    private TansferDaoRepository transferDao;

    @Override
    public TransferRespBean tradeProcess(TransferReqBean reqBean) {
        TransferDao dao = TransferHelper.assemDaoData(reqBean);
        if (this.save(dao)) {
            return getTradeResult(dao);
        } else {
            return TransferHelper.failRespData(reqBean);
        }
    }

    @Override
    public boolean save(TransferDao dao) {
        try {
            transferDao.save(dao);
            return true;
        } catch (Exception e) {
            logger.error("保存订单出错：", e);
            return false;
        }
    }

    @Override
    public TransferRespBean getTradeResult(TransferDao dao) {
        //1.组装ctx
        IPayContext ctx = TransferHelper.assemTradeData(dao);
        //2.获取返回数据
        IBankDirector bank = new EcardDirectorImpl();
        ResponseBean bean = bank.doPay(ctx);
        //3.更新数据库
        updateOrderStatus(bean);

        //组装返回
        TransferRespBean respBean = new TransferRespBean();
        BeanUtils.copyProperties(dao, respBean);
        respBean.setStatusCode(bean.getStatusCode());
        respBean.setAmount(bean.getAmount());
        respBean.setPaytoolCode(bean.getBankCode());
        return respBean;
    }

    @Override
    public boolean updateOrderStatus(ResponseBean respBean) {
        if (respBean != null) {
            String statusCode = (StatusInfo._0000.code().equals(respBean.getStatusCode())) ? "0" : "1";
            String completeTime = TransferHelper.generateCurrTime();
            String serialNo = respBean.getSerialNo();
            String paytoolCode = respBean.getBankCode();
            String orderId = respBean.getOrderNo();
            String amount = respBean.getAmount();
            try {
                return transferDao.updateOrderStatusInfo(statusCode, completeTime, serialNo, paytoolCode, amount, orderId) >= 1;
            } catch (Exception e) {
                logger.error("更新订单[{}]失败", respBean.getOrderNo(), e);
                return false;
            }
        }
        logger.info("转账更新返回结果对象ResponseBean不能为空");
        return false;

    }

}
